layui.define(['index', 'form', 'table', 'laypage', 'layedit', 'laydate'], function (exports) {
    var $ = layui.$;
    var form = layui.form;
    var layer = layui.layer;
    var table = layui.table;
    var laypage = layui.laypage;
    var laydate = layui.laydate;


    zmnLayUtil.layFormVerify(form);

    // 提交
    form.on('submit(zmn-form-submit)', function(obj){
        let data = obj.field;
        let itemList = [];
        //选中的checkbox
        let selected = layui.$(':checkbox:checked:not(.checkAll)');
        selected.each(function(i,item){
            let value = item.value.split('-');
            let oneItem = {provinceId:value[0],cityId:value[1],provinceName:value[2],cityName:value[3]};
            itemList.push(oneItem);
        });
        data.itemList=itemList;
        var url = "/service/tariff/citygroup/addEdit.action";
        $.ajax({
            async: false,
            type: "POST", url: url,
            contentType: "application/json", dataType: "json", data: JSON.stringify(data),
            success:function(resp) {
                var message = resp.status == 200 ? "操作成功！" : resp.message;
                layer.msg(message);
                if(resp.status == 200) {
                    setTimeout(function () {
                        let closeUrl = "/service/tariff/citygroup/detail.action?tariffId="+json.vo.tariffId+"&categOneId="+json.vo.categOneId+"&servCategId="+json.vo.servCategId;
                        if(json.vo.groupId){
                            closeUrl = closeUrl +"&groupId="+json.vo.groupId;
                        }
                        let refreshUrl = "/service/tariff/citygroup/list.action?servCategId="+json.vo.servCategId+"&categOneId="+json.vo.categOneId+"&tariffId="+json.vo.tariffId;
                        parent.layui.index.refreshAndCloseTabs(refreshUrl, closeUrl);
                    },1000);
                }
            },
            error:function(XMLHttpRequest, status, errorThrown){}
        });
        return false;
    });


    form.on('checkbox(checkAll)', function(data){
        var _class = data.value;
        if (data.elem.checked) {
            $("." + _class).each(function(){
                $(this).prop("checked",true);
            });
            form.render('checkbox');
        } else {
            $("." + _class).each(function(){
                $(this).prop("checked",false);
            });
            form.render('checkbox');
        }
        form.render('checkbox');
    });

    var checkAll = function (data) {
        var _class = data.value.substr(0, data.value.indexOf("-"));
        var item = $("." + _class);
        for (var i = 0; i < item.length; i++) {
            if (item[i].checked == false) {
                $("input[value='"+_class+"'").prop("checked", false);
                form.render('checkbox');
                return;
            }
        }
        //如果都勾选了  勾上全选
        var all = item.length;
        for (var i = 0; i < item.length; i++) {
            if (item[i].checked == true) {
                all--;
            }
        }
        if(all == 0){
            $("input[value='"+_class+"'").prop("checked", true);
            form.render('checkbox');
        }
    };

    //有一个未选中全选取消选中
    form.on('checkbox(checkOne)', checkAll);
    $(".checkAll").each(function(){
        checkAll({value : this.value + "-0"});
    })

    //对外暴露的接口
    exports('serv_service_tariff_citygroup_detail', {});
});

